package com.itheima.servletConfig;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

public class ServletDemo08 extends HttpServlet {
    private ServletConfig servletConfig;
/*
演示 ServletConfig 的方法
 *     <servlet>
 *         <servlet-name>ServletDemo08</servlet-name>
 *         <servlet-class>com.itheima.b_servletconfig.ServletDemo08</servlet-class>
 *         <init-param>
 *             <param-name>encoding1</param-name>
 *             <param-value>utf-8</param-value>
 *         </init-param>
 *         <init-param>
 *             <param-name>encoding2</param-name>
 *             <param-value>gbk</param-value>
 *         </init-param>
 *     </servlet>
 *     <servlet-mapping>
 *         <servlet-name>ServletDemo08</servlet-name>
 *         <url-pattern>/ServletDemo08</url-pattern>
 *     </servlet-mapping>
 */


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig servletConfig = this.getServletConfig();
        //1.获取Servlet的名称
        System.out.println(servletConfig.getServletName());
        //2.获取某一个局部初始化参数
        String encoding1 = servletConfig.getInitParameter("encoding");
        System.out.println(encoding1);
        //3.获取所有的局部参数
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            //判断枚举中有没有下一个元素
            String name = initParameterNames.nextElement();
            String value = servletConfig.getInitParameter(name);
            System.out.println(name+":"+value);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
